package com.wss.merchant.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wss.merchant.domain.CommonResult;
import com.wss.merchant.domain.DlNewProduct;
import com.wss.merchant.service.DlNewProductService;
import com.wss.merchant.service.impl.DlNewProductServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品表 前端控制器
 * </p>
 *
 * @author wss
 * @since 2021-02-23
 */
@Controller
@RequestMapping("/product")
@Slf4j              //注解
public class DlNewProductController {
    @Autowired
    DlNewProductServiceImpl dlNewProductService;
    //查询商品
    @RequestMapping("/getProduct")
    @ResponseBody
    public CommonResult getProduct(@RequestBody Map<String,String> map){
        Page<Map> page = new Page<>(Integer.parseInt(map.get("current")),Integer.parseInt(map.get("size")));
        Page<Map> mapPage = dlNewProductService.selectProduct(page, map);
        return new CommonResult(200,"查询成功",mapPage);
    }
    //下架商品
    @RequestMapping("/deleteProduct")
    @ResponseBody
    public CommonResult deleteProduct(@RequestBody Map<String,String> map){
        Integer id = Integer.valueOf(map.get("id"));
        DlNewProduct byId = dlNewProductService.getById(id);
        if(byId.getStatus() == 0) {
            byId.setStatus(1);
        }else if(byId.getStatus() == 1){
            byId.setStatus(0);
        }
        boolean b = dlNewProductService.saveOrUpdate(byId);
        if(b){
            return new CommonResult(200,"更新成功");
        }else {
            return new CommonResult(200,"更新失败");
        }

    }
    //修改
    @RequestMapping("/updateProduct")
    @ResponseBody
    public CommonResult updateProduct(@RequestBody Map<String,Object> map){

        boolean b = dlNewProductService.alterProduct(map);

        if(b == true){
            return new CommonResult(200,"成功");
        }else {
            return new CommonResult(200,"失败");
        }

    }
    //查询指定商家产品
    @RequestMapping("/selectProduct")
    @ResponseBody
    public CommonResult selectProduct(@RequestBody Map<String,String> map){
        Page<Map> page = new Page<>(Integer.parseInt(map.get("current")),Integer.parseInt(map.get("size")));
        Page<DlNewProduct> storeProduct = dlNewProductService.getStoreProduct(page, map);

        return new CommonResult(200,"店铺查询",storeProduct);
    }

    //添加商品
    @RequestMapping("/add")
    @ResponseBody
    public CommonResult addProduct(@RequestBody DlNewProduct product){
        product.setSales(0);
        product.setSee(0);
        product.setStatus(1);
        dlNewProductService.save(product);

        return new CommonResult(200,"商品添加成功");
    }
//    修改商品
    @RequestMapping("/alter")
    @ResponseBody
    public CommonResult alter(@RequestBody DlNewProduct product){
        dlNewProductService.updateById(product);
        return new CommonResult(200,"商品修改成功");
    }

}

